const moment = require('moment')
const { sequelize } = require('../core/db')
const { Sequelize, Model } = require('sequelize')
const User = require('./User')

class Like extends Model {}

Like.init(
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    type: {
      type: Sequelize.STRING(32),
      allowNull: false,
      comment: '点赞的内容'
    },
    target_id: {
      type: Sequelize.INTEGER,
      allowNull: false,
      comment: '那个内容的id'
    },
    created_at: {
      type: Sequelize.DATE,
      allowNull: false,
      get() {
        return moment(this.getDataValue('created_at')).format(
          'YYYY-MM-DD HH:mm'
        )
      }
    },
    updated_at: {
      type: Sequelize.DATE,
      allowNull: false,
      get() {
        return moment(this.getDataValue('updated_at')).format(
          'YYYY-MM-DD HH:mm'
        )
      }
    }
  },
  {
    sequelize,
    modelName: 'like',
    tableName: 'likes'
  }
)

Like.belongsTo(User, {
  foreignKey: 'user_id', as: 'user', targetKey: 'id'
})

module.exports = Like
